//-----------------------------------------------------------------------------
// File: EllipseShape.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

using ObjectiveDraw.Platform.Configuration;


namespace ObjectiveDraw.Platform.Drawing
{
	/// <summary>
	/// Summary description for EllipseShape.
	/// </summary>
	public class EllipseShape : LineSegmentShape
	{		
		
		private float m_AbscissaBeginEllipse;	
		private float m_AbscissaEndEllipse;		

		public EllipseShape()
		{				
		}

		//protected System.Drawing.Color m_EllipseColor;
		//[ObjectiveDraw.Platform.Serialization.SerializableProperty(KnownDrawComponent.EllipseColor)]
		//[ObjectiveDraw.Platform.Serialization.Formatter(typeof(ObjectiveDraw.Platform.Serialization.ColorFormater))]
		public System.Drawing.Color EllipseColor
		{
			get
			{
				return this.m_ForeColor;
			}
			/*set
			{
				this.m_EllipseColor = value;
			}*/
		}

		public override void RenderShape(DrawerChartBase control, ChartInfoArgs cinfo, Cerebrum.Direct3D.Forms.RenderEventArgs re, float z)
		{		
			Microsoft.DirectX.Direct3D.Device device = re.Device;

			float x1 = OffsetFromAbscissa(control, this.AbscissaBegin);
			float x2 = OffsetFromAbscissa(control, this.AbscissaEnd);

			float y1 = OffsetFromOrdinate(control, this.OrdinateBegin);
			float y2 = OffsetFromOrdinate(control, this.OrdinateEnd);

			int clr1 = this.m_MiscColor.ToArgb();

			VertexBuffer VertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored), 2, device, Usage.WriteOnly, CustomVertex.TransformedColored.Format, Pool.SystemMemory);

			device.VertexFormat = CustomVertex.TransformedColored.Format;

			// set the vertexbuffer stream source
			device.SetStreamSource(0, VertexBuffer, 0, VertexInformation.GetFormatSize(CustomVertex.TransformedColored.Format));
			// set fill mode
			device.RenderState.FillMode = FillMode.Solid;


			CustomVertex.TransformedColored[] verts = (CustomVertex.TransformedColored[])VertexBuffer.Lock(0,0); // Lock the buffer (which will return our structs)

			verts[0].Position = new Vector4(x1, y1, z, 1.0f);
			verts[0].Color = clr1;

			verts[1].Position = new Vector4(x2, y2, z, 1.0f);
			verts[1].Color = clr1;

			VertexBuffer.Unlock();


			//use the indices buffer
			device.DrawPrimitives(PrimitiveType.LineList, 0, 1);

			VertexBuffer.Dispose();	

			Cerebrum.Direct3D.Drawing.Graphics g = Cerebrum.Direct3D.Drawing.Graphics.FromDevice(device);

			System.Drawing.RectangleF rect = System.Drawing.RectangleF.FromLTRB(x1, y1, x2, y2);
			System.Drawing.Color clr0;
			object objColor = this.EllipseColor;
			if (objColor!= null)
				clr0 = (System.Drawing.Color)objColor;				
			else
				clr0 = this.m_ForeColor;
			g.DrawEllipse(rect, z, clr0, 9, false);
		
		}


		public override void ClearControlPoints()
		{
			base.ClearControlPoints();
			if(m_MenuItem != null)
			{
				ObjectiveDraw.Platform.Application _app = this.DrawerDocument.DrawerApplication;
				_app.ShapeMenu.MenuItems.Remove(m_MenuItem);
				m_MenuItem = null;
			}
		}
	
		public override void OnMouseMove(DrawerChartBase control, MouseEventArgs e)
		{	
			if(this.Creating)
			{	
				m_AbscissaBeginEllipse = AbscissaBegin;
				m_AbscissaEndEllipse = AbscissaEnd;
			}
	
			base.OnMouseMove (control, e);
			if(!this.Creating)
			{	
				AbscissaBegin = m_AbscissaBeginEllipse;
				AbscissaEnd = m_AbscissaEndEllipse;
			}
		}
	
		protected override void OnControlPointMouseMove(object sender, MouseEventArgs e)
		{			
			base.OnControlPointMouseMove (sender, e);
			
			m_AbscissaBeginEllipse = base.AbscissaBegin;
			m_AbscissaEndEllipse = base.AbscissaEnd;
		}
	
		protected override void AddControlPoint(object key, Cerebrum.Direct3D.Forms.Control controlPoint)
		{
			base.AddControlPoint (key, controlPoint);
			ObjectiveDraw.Platform.Application _app = this.DrawerDocument.DrawerApplication;
			if(_app.ShapeMenu!=null)
			{
				controlPoint.ContextMenu = _app.ShapeMenu;	
				if (!controlPoint.ContextMenu.MenuItems.Contains(m_MenuItem))
					m_MenuItem = controlPoint.ContextMenu.MenuItems.Add("Format Object ...", new System.EventHandler(OnFormatObject));
			}
		}

		private System.Windows.Forms.MenuItem m_MenuItem = null;
		

		private void OnFormatObject(object sender, System.EventArgs e)
		{					
			//--Cerebrum.Integrator.IComponent cfg = m_SystemContext.AcquireComponent(ObjectiveDraw.Platform.KnownDrawerComponents.DrawerConfigurator) as Cerebrum.Integrator.IComponent;						

			//--ConfigurationElement root = new ConfigurationElement();
			//--root.Elements.Add(Tools.BuildEllipeTree(this));			
			//--cfg.Execute(this, new ConfigureInstanceEventArgs(this.SystemContext.PrimaryWindow.GetCommand(ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationUpdate), this ,root));// element));
			//--ObjectiveDraw.Platform.Workspace.ConfigureComponent(this, ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationUpdate, root);
		}

	
		protected override bool InitVariables(bool loading)
		{	
			bool baseValue = base.InitVariables(loading);
			/*--if (m_Variables == null)
				m_Variables = new System.Collections.Hashtable ();
			Workspace.SaveVariable(this, KnownDrawComponent.EllipseColor, m_ForeColor);	
			m_Variables.Add( KnownDrawComponent.EllipseColor,m_ForeColor);			--*/
			//this.EllipseColor = this.m_ForeColor;
			return baseValue;
		}
	
		protected override bool LoadVariables(ObjectiveDraw.Platform.Serialization.FallbackChain storage, SerializationContext context)
		{
			bool baseValue = base.LoadVariables(storage, context);

			m_AbscissaBeginEllipse = AbscissaBegin;
			m_AbscissaEndEllipse = AbscissaEnd;

			/*--if (m_Variables == null)	
				m_Variables = new System.Collections.Hashtable (); 						
			object o = Workspace.LoadVariable(false, this, KnownDrawComponent.EllipseColor);		
			if (o != null)
				m_Variables.Add(KnownDrawComponent.EllipseColor,(System.Drawing.Color)o);	
			else
				m_Variables.Add(KnownDrawComponent.EllipseColor, m_ForeColor);--*/
			return baseValue;
		}	
		
	}
}
